var _G = {
    ioc: new Set(),
    show: 45,
    rx_port_on: [],
    timer : {},
    charts : new Array(4),
    sdrs: {},
    MaxPorts: 4,
};

function clearAllTimer() {
    Object.values(_G.timer).forEach(function(t) {clearInterval(t)});
};

class Req {
    constructor(f,cb) {
        this.f = f
        this.cb = cb
    };
    then(f,cb) {
        this.t = new Req(f,cb);
        return this.t;
    };
    do() {
        this.f((d)=>{
            this.cb(d);
            this.t && this.t.do();
        })
    };
}

function requrl(url,urldata) {
    return function(cb) {
        $.ajax({
            type: "GET",
            url: url,
            dataType: 'json',
            data: urldata,
            success: function (result) {
                cb(result.data)
            }
        });
    }
}

function reqrhd(route,handle,urldata) {
    url = "/"+route+"/"+handle
    console.log(url)
    return requrl(url,urldata)
}

class View {
    constructor(rxp) {
        this.rxp = rxp;
        this.rf = GetRFDevice(rxp);
        this.viewFunction = [ 'afc'
                            , 'resetppm'
                        ];
    };
    open(module,mode) {
        this.view = new Data(module);
        this.view.mode = mode
        this.view.port = this.rxp
        return reqrhd('new',this.view.handle(this.rxp),this.view); 
    };
    Do(r,d) {
        return reqrhd(r,this.view.handle(this.rxp),d);
    };
    viewDo(r,d) {
        if (this.viewFunction.includes(r)) {
            return this.Do(r,d);   
        }
    };
    resetPPM() {
        return this.viewDo("resetppm",{});
    };
    afcOn() {return this.viewDo("afc",{on:true})};
    afcOff() {return this.viewDo("afc",{on:false})};
};

class Data {
    constructor(module) {
        this.module = module;
    };
    urldata() {
        var sj = JSON.parse(JSON.stringify(this));
        return encodeURI(sj);
    };
    handle(index) {
        return this.module+index
    };
};

class Device extends Data {
    constructor(mode) {
        super('dev');
        this.mode = mode;
        this.sr = 1.92e6;
        this.gain = 40.0;
        if (this.mode=='lte') {
            this.fc = 874.2e6
        } else {
            this.fc = 104.1e6;
        }
    }
};

class CellData extends Data {
    constructor(cid,txp,pos) {
        this.cid = cid;
        this.txp = txp;
        this.pos = pos;
    }
};
class RF {
    constructor(port) {
        this.port = port;
        this.dev = null;
        this.devSimpleFunction = ['start','suspend','resume'];
        this.devSetFunction = ['fc',"gain"];
    }
    open(mode) {
        this.dev = new Device(mode)
        this.dev.port = this.port;
        return reqrhd('new',this.dev.handle(this.port),this.dev)    
    };
    simpleDev(r) {
        if (this.devSimpleFunction.includes(r)) {
            return reqrhd(r,this.dev.handle(this.port),{port:this.port})
        }
    };
    setDev(r,v) {
        if (this.devSetFunction.includes(r)) {
            return reqrhd(r,this.dev.handle(this.port),{r:v,port:this.port})
        }
    };
}

var RFDevices = new Array();

function InitRxPort(cb) {
    requrl("plist",{})(function(data){
        for (var h in data) {
            RFDevices.push(new RF(h))
            $.ajax({
                type: "GET",
                url: "/rx_port",
                dataType: 'html',
                data:{port:h},
                success: function (result) {
                    $("#rx_config").append(result);
                    var el = $("#rx_config li:last")
                    var rx = el.attr('id')
                    var rxp = parseInt(rx.slice(2))
                    cb(rxp,el.find('#view'))
                }
            });
        }
    })
};

function GetRFDevice(rxp) {
    return RFDevices[rxp] || null;
}
